<template>
  <div class="home">
    <el-card class="box-card" shadow="hover">
      <div style="display: flex; margin-bottom: 10px">
        <span style="font-weight: bold">分类详情</span>
        <el-button
          style="margin-left: 3rem;"
          type="primary"
          @click="SearchParentList"
        >
          查询父类
        </el-button>

        <el-button style="margin-left: 10px" @click="reset" type="primary">
          <i class="el-icon-refresh-right"></i>重置
        </el-button>
        <el-button
          style="margin-left: auto; margin-right: 40px"
          type="primary"
          @click="add"
        >
          <i class="el-icon-circle-plus-outline"></i>
          添加商品分类
        </el-button>
      </div>

      <el-table
        :data="tableData"
        stripe
        border
        height="500"
        row-key="shopCategoryId"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          prop="id"
          label="分类Id"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="分类名称"
        ></el-table-column>
        <el-table-column
            prop="parentId"
            label="父类Id"
        ></el-table-column>
        <el-table-column
            prop="sortOrder"
            label="优先级"
        ></el-table-column>

        <el-table-column
            prop="createdUser"
            label="创建人"
        ></el-table-column>
        <el-table-column
            prop="createdTime"
            label="创建时间"
        >
          <template slot-scope="scope">
            {{ scope.row.createdTime | formatTime }}
          </template>
        </el-table-column>

        <el-table-column
            prop="modifiedUser"
            label="修改人"
        ></el-table-column>
        <el-table-column
            prop="modifiedTime"
            label="修改时间"
        >
          <template slot-scope="scope">
            {{ scope.row.modifiedTime | formatTime }}
          </template>
        </el-table-column>


        <el-table-column
            prop="status"
            label="状态"
        ></el-table-column>

        <el-table-column label="操作" width="250">
          <template v-slot="scope">
            <el-button type="text" @click="edit(scope.row)">
              <i class="el-icon-edit"></i>
              编辑商品分类
            </el-button>
            <el-popconfirm
              style="margin-left: 4px"
              title="您确定删除这行数据吗？"
              @confirm="del(scope.row.id)"
            >
              <el-button
                slot="reference"
                type="text"

              >
                <i class="el-icon-delete"></i>删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--  分页-->
      <div style="margin-top: 20px; display: flex">
        <el-pagination
          background
          :current-page="params.pageIndex"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>



    <el-dialog
        :visible.sync="IsAdd"
        title="新增分类"
        width="60%"
        :before-close="handleClose"
    >
      <el-form :model="addForm" ref="form"  label-width="80px">
        <el-row>
          <!-- 左侧输入框 -->
          <el-col :span="12">
            <!-- 商品系列 -->
            <el-form-item label="分类名称">
              <el-input v-model="addForm.name"></el-input>
            </el-form-item>
          </el-col>
          <!-- 商品标题 -->
          <el-col :span="12">
            <el-form-item label="父类Id" >
              <el-input v-model="addForm.parentId"></el-input>
            </el-form-item>
          </el-col>

        </el-row>



        <!-- 商品状态和商品优先级 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品状态" >
              <el-select v-model="addForm.status" placeholder="请选择" style="width: 345px">
                <el-option label="上架" value="1"></el-option>
                <el-option label="下架" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 商品卖点 -->
            <el-form-item label="优先级" >
              <el-input v-model="addForm.sortOrder"></el-input>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="IsAdd = false">取 消</el-button>
        <el-button type="primary" @click="addMsg">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改商品分类" :visible.sync="IsEdit" width="60%">
      <el-form :model="editForm" ref="editform"  label-width="80px">
        <el-row>
          <!-- 左侧输入框 -->
          <el-col :span="12">
            <!-- 商品系列 -->
            <el-form-item label="分类名称" prop="itemType">
              <el-input v-model="editForm.name"></el-input>
            </el-form-item>
          </el-col>
          <!-- 商品标题 -->
          <el-col :span="12">
            <el-form-item label="父类Id" prop="title">
              <el-input v-model="editForm.parentId"></el-input>
            </el-form-item>
          </el-col>

        </el-row>

        <!-- 商品状态和商品优先级 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品状态" prop="status">
              <el-select v-model="editForm.status" placeholder="请选择" style="width: 345px">
                <el-option label="上架" value="1"></el-option>
                <el-option label="下架" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 商品卖点 -->
            <el-form-item label="优先级" prop="sellPoint">
              <el-input v-model="editForm.sortOrder"></el-input>
            </el-form-item>
          </el-col>
        </el-row>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="IsEdit = false">取 消</el-button>
        <el-button type="primary" @click="editMsg">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

    <script>
import request from "@/api/request";
//   import Cookies from "js-cookie";

export default {
  name: "StudentList",
  data() {
    return {
      categoryList: [],
      specificationList: [],
      addForm: {
        name: "",
        parentId: "",
        sortOrder: "",
        status: "",
      },
      editForm:{
        name: "",
        parentId: "",
        sortOrder: "",
        status: "",
      },
      // 添加
      IsAdd: false,
      IsEdit: false,
      // 展示

      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      params: {
        pageSize: 10,
        pageIndex: 1,
        isParent: 0
      },
    };
  },
  filters: {
    formatTime: function(value) {
      if (!value) return '';
      const date = new Date(value);
      return date.toLocaleString(); // 或者使用其他合适的时间格式化方法
    }
  },
  created() {
    this.load();
    this.getPid();
  },
  methods: {

    addMsg() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.Addform);
          request.post("/api/category/add", this.addForm).then((res) => {
            if (res.code === 20000) {
              this.$notify.success("新增成功");
              this.IsAdd = false;
              this.load();
            } else {
              this.$notify.error(res.msg);
            }
          });
        }
      });
    },

    editMsg() {
      this.$refs["editform"].validate((valid) => {
        if (valid) {
          console.log(this.editForm)
          request.put("/api/category/update", this.editForm).then((res) => {
            if (res.code === 20000) {
              this.$notify.success("更新成功");
              this.IsEdit = false;
              this.load();
            } else {
              this.$notify.error(res.msg);
            }
          });
        }
      });
    },
    edit(row) {
      this.IsEdit = true;
      request.get("/api/category/get?id=" + row.id).then((res) => {
        console.log(res.data);
        this.editForm = res.data;
      });
    },
    load() {
      request
          .post("/api/category/page", this.params)
          .then((res) => {
            console.log(res);
            if (res.code === 20000) {
              console.log(res)
              this.tableData = res.data.list;
              console.log(this.tableData);
              this.total = res.data.total;
            }
          });
    },
    reset() {
      this.params = {
        pageSize: 10,
        pageIndex: 1,
        isParent: 0,
      };
      this.load();
    },
    SearchParentList() {
      this.params.isParent = 1;
      this.load();
    },
    // 换页
    handleCurrentChange(pageNum) {
      this.params.pageIndex = pageNum;
      this.load();
    },
    add() {
      this.IsAdd = true;
      //清空表单
      this.$refs["form"].resetFields();
    },
    del(categoryId) {
      request.delete("/api/category/delete?categoryId=" + categoryId)
          .then((res) => {
            if (res.code === 20000) {
              this.$notify.success("删除成功");
              this.load();
            } else {
              this.$notify.error(res.msg);
            }
          })
          .catch((error) => {
            console.error("删除请求出错:", error);
          });
    }
  },
};
</script>

    <style scoped>
.el-form-item__label {
  align-items: flex-start;
  box-sizing: border-box;
  color: var(--el-text-color-regular);
  display: inline-flex;
  flex: 0 0 auto;
  font-size: var(--el-form-label-font-size);
  height: 32px;
  justify-content: flex-end;
  line-height: 32px;
  padding: 0 12px 0 0;
  font-weight: bold;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
